<template>
  <div class="user-profile">
    <div class="header">
      <h1>个人中心</h1>
    </div>
    
    <el-card class="profile-card">
      <div class="profile-header">
        <div class="avatar">
          <el-avatar :size="100" :src="userStore.userInfo?.avatar || '/default-avatar.png'"></el-avatar>
        </div>
        <div class="user-info">
          <h2>{{ userStore.userInfo?.username || '用户名' }}</h2>
          <p>{{ userStore.userInfo?.email || 'user@example.com' }}</p>
        </div>
      </div>
      
      <el-tabs v-model="activeTab">
        <el-tab-pane label="基本信息" name="basic">
          <el-form :model="userForm" label-width="100px">
            <el-form-item label="用户名">
              <el-input v-model="userForm.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="userForm.email"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="userForm.phone"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="saveUserInfo">保存修改</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="修改密码" name="password">
          <el-form :model="passwordForm" label-width="100px">
            <el-form-item label="原密码">
              <el-input v-model="passwordForm.oldPassword" type="password" show-password></el-input>
            </el-form-item>
            <el-form-item label="新密码">
              <el-input v-model="passwordForm.newPassword" type="password" show-password></el-input>
            </el-form-item>
            <el-form-item label="确认密码">
              <el-input v-model="passwordForm.confirmPassword" type="password" show-password></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="changePassword">修改密码</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="我的订单" name="orders">
          <div class="order-stats">
            <el-row :gutter="20">
              <el-col :span="6">
                <div class="stat-item">
                  <div class="stat-value">{{ orderStats.total || 0 }}</div>
                  <div class="stat-label">全部订单</div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="stat-item">
                  <div class="stat-value">{{ orderStats.pendingPayment || 0 }}</div>
                  <div class="stat-label">待付款</div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="stat-item">
                  <div class="stat-value">{{ orderStats.pendingDelivery || 0 }}</div>
                  <div class="stat-label">待发货</div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="stat-item">
                  <div class="stat-value">{{ orderStats.pendingReceive || 0 }}</div>
                  <div class="stat-label">待收货</div>
                </div>
              </el-col>
            </el-row>
          </div>
          
          <div class="order-actions">
            <el-button type="primary" @click="goToOrders">查看所有订单</el-button>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../store'

const router = useRouter()
const userStore = useUserStore()

// 当前激活的标签页
const activeTab = ref('basic')

// 用户表单
const userForm = reactive({
  username: '',
  email: '',
  phone: ''
})

// 密码表单
const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 订单统计
const orderStats = reactive({
  total: 0,
  pendingPayment: 0,
  pendingDelivery: 0,
  pendingReceive: 0
})

// 保存用户信息
const saveUserInfo = () => {
  // 这里应该调用API保存用户信息
  ElMessage.success('保存成功')
}

// 修改密码
const changePassword = () => {
  if (passwordForm.newPassword !== passwordForm.confirmPassword) {
    ElMessage.error('两次输入的密码不一致')
    return
  }
  
  if (passwordForm.newPassword.length < 6) {
    ElMessage.error('密码长度不能少于6位')
    return
  }
  
  // 这里应该调用API修改密码
  ElMessage.success('密码修改成功')
  
  // 清空表单
  passwordForm.oldPassword = ''
  passwordForm.newPassword = ''
  passwordForm.confirmPassword = ''
}

// 跳转到订单列表
const goToOrders = () => {
  router.push('/orders')
}

// 页面加载时初始化数据
onMounted(() => {
  // 加载用户信息
  if (userStore.userInfo) {
    userForm.username = userStore.userInfo.username || ''
    userForm.email = userStore.userInfo.email || ''
    userForm.phone = userStore.userInfo.phone || ''
  }
  
  // 加载订单统计（模拟数据）
  orderStats.total = 12
  orderStats.pendingPayment = 2
  orderStats.pendingDelivery = 1
  orderStats.pendingReceive = 3
})
</script>

<style scoped>
.user-profile {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  margin-bottom: 30px;
}

.header h1 {
  font-size: 28px;
}

.profile-card {
  margin-bottom: 20px;
}

.profile-header {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.avatar {
  margin-right: 30px;
}

.user-info h2 {
  font-size: 24px;
  margin-bottom: 10px;
}

.user-info p {
  color: #666;
  font-size: 16px;
}

.order-stats {
  margin-bottom: 30px;
}

.stat-item {
  text-align: center;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.order-actions {
  text-align: center;
}
</style>